package com.roger.myHome.service.impl;

import com.roger.myHome.domain.TbUsers;
import com.roger.myHome.mapper.UsersMapper;
import com.roger.myHome.service.CacheService;
import com.roger.myHome.service.EhcacheService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CacheServiceImpl implements CacheService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private EhcacheService ehcacheService;

    @Override
    @Cacheable(cacheNames = "userCache", key  = "#id")
    public TbUsers findById(Long id) {
        //System.err.println(System.currentTimeMillis() + ": 未命中缓存，请求数据库");
        return usersMapper.selectById(id);
    }

    @Override
    public List queryUserList(Integer id){
        return ehcacheService.queryUserList(id);
    }

    @CacheEvict(value="userCache",key="#id")
    public void removeCache(Long id) {
        System.out.println("remove cache:" + id);
    }
}
